import pygame
from pygame.sprite import Sprite


class Player(Sprite):
    def __init__(self, game):
        super().__init__()
        # get main screen info
        self.main_screen = game.screen
        self.main_screen_rect = game.screen.get_rect()
        # load image and get hitbox
        self.image = pygame.image.load('_image/player.png').convert()
        self.rect = self.image.get_rect()
        # set position
        self.rect.midbottom = self.main_screen_rect.midbottom
        # movement flag
        self.move_up = False
        self.move_left = False
        self.move_down = False
        self.move_right = False
        # move speed
        self.player_x_speed = 4.5
        self.player_y_speed = 3.0
        self.x = float(self.rect.x)
        self.y = float(self.rect.y)
        # player stats
        self.health = 3

    def update(self):
        if self.move_up and self.rect.top > 0:
            self.y -= self.player_y_speed
            self.rect.y = self.y
        if self.move_down and self.rect.bottom < self.main_screen_rect.bottom:
            self.y += self.player_y_speed
            self.rect.y = self.y
        if self.move_left and self.rect.left > 0:
            self.x -= self.player_x_speed
            self.rect.x = self.x
        if self.move_right and self.rect.right < self.main_screen_rect.right:
            self.x += self.player_x_speed
            self.rect.x = self.x

    def blit(self):
        # print player in designated position
        self.main_screen.blit(self.image, self.rect)
